package com.xiaoyu.base.app;

import android.app.Activity;
import android.app.Application;

import androidx.annotation.Nullable;

import java.lang.ref.WeakReference;

public class App {

    private static App sInstance;
    private Application mApplication;
    private WeakReference<Activity> mTopActivityReference;

    public static synchronized App getInstance() {
        if (sInstance == null) {
            sInstance = new App();
        }
        return sInstance;
    }

    public Application getApplication() {
        return mApplication;
    }

    @Nullable
    public Activity getTopActivity() {
        if (mTopActivityReference == null) {
            return null;
        }
        return mTopActivityReference.get();
    }

    public void onActivityResume(Activity activity) {
        mTopActivityReference = new WeakReference<>(activity);
    }

    public App onCreate(Application app) {
        mApplication = app;
        return this;
    }
}
